package com.xii.eduVod.service.impl;


import com.aliyun.oss.ClientException;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.xii.eduVod.service.VideoService;
import com.xii.eduVod.utils.AliyunVodSDKUtils;
import com.xii.eduVod.utils.ConstantPropertiesUtil;
import com.xii.servicebase.exceptionhandler.XiiException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @description: 上传视频实现类
 *
 * @author  wangxihao
 * @email wangxh0108@163.com
**/
@Service
public class VideoServiceImpl implements VideoService {

    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            //文件上传的输入流
            InputStream inputStream = file.getInputStream();
            //上传文件原始的名字
            String originalFilename = file.getOriginalFilename();
            //title  文件上传后的标题
            String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
            //文件上传
            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET,
                    title, originalFilename, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。
            // 其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            String videoId = response.getVideoId();
            if (!response.isSuccess()) {
                String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
//                log.warn(errorMessage);
                System.out.println(errorMessage);
                if(StringUtils.isEmpty(videoId)){
                    throw new XiiException(20001, errorMessage);
                }
            }
            return videoId;
        } catch (IOException e) {
            throw new XiiException(20001, "guli vod 服务上传失败");
        }
    }

    /**
     * @description: 删除视频
     * 
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @Override
    public void removeVideo(String videoId) {
        try{
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoId);
            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.print("RequestId = " + response.getRequestId() + "\n");
        }catch (ClientException | com.aliyuncs.exceptions.ClientException e){
            throw new XiiException(20001, "视频删除失败");
        }
    }


    @Override
    public void removeMoreAlyVideo(List videoIdList) {
        try {
            //初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //videoIdList值转换成 1,2,3
            String videoIds = org.apache.commons.lang.StringUtils.join(videoIdList.toArray(), ",");

            //向request设置视频id
            request.setVideoIds(videoIds);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        }catch(Exception e) {
            e.printStackTrace();
            throw new XiiException(20001,"删除视频失败");
        }
    }
}
